<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of registro_model
 *
 * @author Neil
 */
class registro_model extends CI_Model {

    public function regRegistro($datos, $monto) {
        $this->db->trans_start();
        $this->db->insert('registro', $datos);
        $query = $this->db->query('SELECT max(idRegistro) as idReg FROM registro');
        $dato = $query->result_array();
        $dataDetMon = array(
            'registro' => $dato{0}['idReg'],
            'submonto' => $monto,
        );
        $this->regPrimerSubmonto($dataDetMon);
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            
        }
    }

    public function listRegistros($reg,$fec1,$fec2) {
        if ($reg != "") {
            $query = $this->db->query('select c.nombre, c.dni, h.nro_habi,r.fec_ing, r.monto, r.obs from Cliente c inner join registro r 
                                    on c.cod_cli = r.cliente inner join habitacion h 
                                    on r.habitacion = h.id_habi where idRegistro = ?', $reg);
        }else{
            if ($fec2 == "") {
                $query= $this->db->query("select r.idRegistro,c.nombre, c.dni,h.nro_habi,r.fec_ing,p.nom_per from cliente c inner join registro r
                                    on c.cod_cli=r.cliente inner join personal p
                                    on p.id_pers = r.personal inner join habitacion h
                                    on r.habitacion = h.id_habi where fec_ing = '$fec1'");
            }else{
                $query= $this->db->query("select r.idRegistro,c.nombre, c.dni,h.nro_habi,r.fec_ing,p.nom_per from cliente c inner join registro r
                                    on c.cod_cli=r.cliente inner join personal p
                                    on p.id_pers = r.personal inner join habitacion h
                                    on r.habitacion = h.id_habi where fec_ing between '$fec1' and '$fec2'");
            }
            
        }
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function regPrimerSubmonto($datos) {
        $this->db->insert('detalle_monto', $datos);
    }

    public function detalleMonto($reg) {
        $this->db->where('registro', $reg);
        $query = $this->db->get('detalle_monto');
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }

    public function regDetalleMonto($reg, $datos, $data) {
        $this->db->where('registro', $reg);
        $query = $this->db->get('detalle_monto');
        if ($query->num_rows() > 0) {
            $this->db->delete("detalle_monto", array("registro" => $reg));
        }
        $this->db->insert_batch('detalle_monto', $datos);
        $this->db->where('idRegistro', $reg);
        $this->db->update('registro', $data);
    }

    public function monto($id) {
        $query = $this->db->query('select SUM(submonto) as submonto, monto from registro r inner join detalle_monto dm 
                                    on r.idRegistro = dm.registro where registro=?', $id);
        return $query->result_array();
    }

}

?>
